package com.chint.largeData;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.chint.largeData.Common.Constant;
import com.chint.largeData.client.MyWebChromeClient;
import com.chint.largeData.client.MyWebViewClient;
import com.chint.largeData.tools.ExampleUtil;
import com.chint.largeData.tools.SharePreferencesUtil;
import com.chint.largeData.tools.StringTools;
import com.chint.largeData.views.ProgressWebView;

public class HomePage extends FragmentActivity{
	private ProgressWebView webView;
	private final int type=1;
	private WebSettings webSettings;
	private long time1 = System.currentTimeMillis();
	//for receive customer msg from jpush server
	private MessageReceiver mMessageReceiver;
	
	public static boolean isForeground = false;
	
	Handler testHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			String mLockCode = StringTools.getstring(SharePreferencesUtil.getInstance(HomePage.this).getValue(Constant.SP_KEY.LOCK_CODE));
			boolean isLocked = !TextUtils.isEmpty(mLockCode);
//	    	Toast.makeText(this, "isLock is called:"+isLocked,Toast.LENGTH_LONG).show();
	    	webView.loadUrl("javascript:reportJs('"+isLocked+"');");
//	    	webView.loadUrl("javascript:aaatest();");
	    	Log.e("LXL", "test call"); 
		};
	};
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		String appKey = ExampleUtil.getAppKey(getApplicationContext());
		setContentView(R.layout.home_page);
		
		webView=(ProgressWebView) findViewById(R.id.webView);
		
		init();
		registerMessageReceiver();
		testHandler.sendEmptyMessage(1);
		
	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		time1 = System.currentTimeMillis();
		Log.e("LXL", "onActivityResult:"+arg0+","+arg1);
		if(arg1==-1){
			finish();
			return;
		}
		if(arg0==1){
//			webView.loadUrl("javascript:platformFunction();");
			webView.loadUrl("javascript:autoLogin('"+getUserName()+"','"+getPassWord()+"');");
			Log.e("LXL", "autoLogin:"+getUserName()+","+getPassWord());
			time1 = System.currentTimeMillis();
		}
	}
	private void init(){
		webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);

		// 设置可以支持缩放  
        webSettings.setSupportZoom(true);  
        // 设置出现缩放工具  
        webSettings.setBuiltInZoomControls(true);  
        webSettings.setDisplayZoomControls(false);
        webSettings.setUseWideViewPort(false);  
        
        webView.setInitialScale(250);
        
		webSettings.setSavePassword(false);
		setWebView("https://bi.chint.com:8443/ChintData/login.jsp");//跳转到登录页面
		//获取用户是否设置了手势密码
		String mLockCode = StringTools.getstring(SharePreferencesUtil.getInstance(this).getValue(Constant.SP_KEY.LOCK_CODE));
		if(StringTools.isEmpty(mLockCode)){//表示设置手势密码 跳转到验证手势页面
			Intent i =new Intent(this,MainActivity.class);
			Log.e("LXL", "startmain 1");
			startActivityForResult(i, type);
		}
		
	}
	@Override
	protected void onResume() {
		isForeground = true;
		super.onResume();
		JPushInterface.onResume(this);
		Log.e("LXL", "HomePage onResume");
		if(Math.abs(System.currentTimeMillis()-time1)>30*1000){
			Log.e("LXL", "HomePage onResume 2");
			Intent i =new Intent(this,MainActivity.class);
			Log.e("LXL", "startmain 2");
			startActivityForResult(i, type);
		}
		
	}


	@Override
	protected void onPause() {
		isForeground = false;
		super.onPause();
		JPushInterface.onPause(this);
		time1 = System.currentTimeMillis();
	}


	@Override
	protected void onDestroy() {
		unregisterReceiver(mMessageReceiver);
		super.onDestroy();
	}
	


	
	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MessageReceiver.MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}

	 /**
     * 设置WebView
     */
    public void setWebView(String url) {
        // 初始化WebView
        webView.loadUrl(url);
        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());
        // 提供JS方法调用安卓接口

		webView.loadUrl("javascript:platformFunction()");
//        webView.loadUrl("javascript:(fun(){window.platform = 'android';})();");
        webView.addJavascriptInterface(HomePage.this,"Login");
        webView.addJavascriptInterface(HomePage.this,"Lock");
        
        
    }
     /**
     * 获取密码和用户名 并且跳转到设置手势密码
     */
    @JavascriptInterface
    public void setPassword(String userName,String passWord){
    	SharePreferencesUtil.getInstance(this).setValue(SharePreferencesUtil.USERNAME, userName);
    	SharePreferencesUtil.getInstance(this).setValue(SharePreferencesUtil.PASSWORD, passWord);
    	Intent i =new Intent(this,MainActivity.class);
    	Log.e("LXL", "startmain 3");
		startActivityForResult(i, type);
//    	Toast.makeText(this, "setPassword", Toast.LENGTH_LONG).show();
    }
    @JavascriptInterface
    public void isLock(){
    	
    	testHandler.sendEmptyMessage(1);
 
    }
    
    @JavascriptInterface
    public void resetLock(){
		SharePreferencesUtil.getInstance(this).remove(Constant.SP_KEY.LOCK_CODE);
		testHandler.post(new Runnable() {
			
			@Override
			public void run() {
				Intent i =new Intent(HomePage.this,MainActivity.class);
				startActivityForResult(i, type);
			}
		});
    }
    
     /**
     * 重置密码
     */
    @JavascriptInterface
    public void resetPassword(String userName,String passWord){
    	SharePreferencesUtil.getInstance(this).setValue(SharePreferencesUtil.USERNAME, userName);
    	SharePreferencesUtil.getInstance(this).setValue(SharePreferencesUtil.PASSWORD, passWord);
//    	Intent i =new Intent(this,MainActivity.class);
//		startActivityForResult(i, type);
    	Toast.makeText(this, "resetPassword", Toast.LENGTH_LONG).show();
    }
     /**
     * 返回用户名
     * @return
     */

    public String getUserName(){
    	return SharePreferencesUtil.getInstance(this).getValue(SharePreferencesUtil.USERNAME);
    }
     /**
     * 返回密码
     * @return
     */
    public String getPassWord(){
    	return SharePreferencesUtil.getInstance(this).getValue(SharePreferencesUtil.PASSWORD);
    }
    
   
}
